{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:green\">Fixed</span>: Can not use a method of an instance if the instance and the type are defined separately\n",
    "- [bug](https://github.com/yunabe/lgo/issues/11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type Data struct {\n",
    "    value string\n",
    "}\n",
    "\n",
    "func (d Data) Value() string {\n",
    "    return d.value\n",
    "}\n",
    "\n",
    "type WithValue interface {\n",
    "    Value() string\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d := Data{\"hello\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "// Got a link error because of https://github.com/golang/go/issues/22998\n",
    "d.Value()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "// This works without any problem.\n",
    "WithValue(d).Value()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:green\">Fixed</span>: goroutines should be canceled when the main routine ends\n",
    "- [bug](https://github.com/yunabe/lgo/issues/6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Canceled\n"
     ]
    }
   ],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    ")\n",
    "\n",
    "i := 0\n",
    "go func() {\n",
    "    defer func() {\n",
    "        fmt.Println(\"Canceled\")\n",
    "    }()\n",
    "    for i = 0;; i++ {}\n",
    "}()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:green\">Fixed</span>: The entire process terminated when a go routine panics\n",
    "- [bug](https://github.com/yunabe/lgo/issues/5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "panic: die!\n",
      "\n",
      "goroutine 26 [running]:\n",
      "runtime/debug.Stack(0xc4203f9eb8, 0x7fc12a067bc0, 0xc42003a010)\n",
      "\t/usr/lib/go-1.8/src/runtime/debug/stack.go:24 +0x7b\n",
      "github.com/yunabe/lgo/core.FinalizeGoRoutine()\n",
      "\t/home/yunabe/local/gocode/src/github.com/yunabe/lgo/core/core.go:81 +0x64\n",
      "panic(0x7fc12a067bc0, 0xc42003a010)\n",
      "\t/usr/lib/go-1.8/src/runtime/panic.go:489 +0x2e7\n",
      "github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8.lgo_init.func1.1()\n",
      "\t/home/yunabe/local/gocode/src/github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8/src.go:13 +0x66\n",
      "github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8.lgo_init.func1()\n",
      "\t/home/yunabe/local/gocode/src/github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8/src.go:14 +0x40\n",
      "created by github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8.lgo_init\n",
      "\t/home/yunabe/local/gocode/src/github.com/yunabe/lgo/sess7b2274696d65223a313530383530393332323132383131303736397d/exec8/src.go:15 +0x3b\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "main done\n",
      "10\n",
      "<nil>\n"
     ]
    }
   ],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    "    \"time\"\n",
    ")\n",
    "\n",
    "go func() {\n",
    "    panic(\"die!\")\n",
    "}()\n",
    "\n",
    "time.Sleep(100 * time.Millisecond)\n",
    "fmt.Println(\"main done\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:green\">Fixed</span>: interface is not working\n",
    "If you invoke a method through an interface in lgo, it crashes with `runtime error: invalid memory address or nil pointer dereference` ([bug](https://github.com/yunabe/lgo/issues/3))."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---- 1 ----\n",
      "Hello, I'm yunabe.\n",
      "---- 2 ----\n",
      "Hello, I'm yunabe.\n"
     ]
    }
   ],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    ")\n",
    "\n",
    "type Hello interface {\n",
    "    SayHello()\n",
    "}\n",
    "\n",
    "type person struct {\n",
    "    name string\n",
    "}\n",
    "\n",
    "func (p *person) SayHello() {\n",
    "    fmt.Printf(\"Hello, I'm %s.\\n\", p.name)\n",
    "}\n",
    "\n",
    "p := person{\"yunabe\"}\n",
    "fmt.Println(\"---- 1 ----\")\n",
    "p.SayHello()\n",
    "\n",
    "var h Hello = &p\n",
    "fmt.Println(\"---- 2 ----\")\n",
    "h.SayHello()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:green\">Fixed</span>: GC crashes with a fatal error\n",
    "The following crashes with `fatal error: found bad pointer in Go heap (incorrect use of unsafe or cgo?)` message ([bug](https://github.com/yunabe/lgo/issues/2))."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    "    \"log\"\n",
    "    \"runtime\"\n",
    "    \"runtime/debug\"\n",
    ")\n",
    "\n",
    "type MyData struct {\n",
    "    b []byte\n",
    "}\n",
    "\n",
    "func (m *MyData) Size() int {\n",
    "    return len(m.b)\n",
    "}\n",
    "\n",
    "func NewMyData() *MyData {\n",
    "    return &MyData{\n",
    "        b: make([]byte, 10 * (1 << 20)),\n",
    "    }\n",
    "}\n",
    "\n",
    "var l []*MyData\n",
    "for i := 0; i < 100; i++ {\n",
    "    d := NewMyData()\n",
    "    l = append(l, d)\n",
    "}\n",
    "l = nil\n",
    "debug.FreeOSMemory()\n",
    "runtime.GC()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:green\">Fixed</span>: functions and arguments in go statement are not bound immediately\n",
    "The following code must output `goroutine: 0`, `goroutine: 1` and `goroutine: 2` in a random order.\n",
    "But `lgo` shows `goroutine: 3` three times ([bug](https://github.com/yunabe/lgo/issues/73))."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "goroutine: 2\n",
      "goroutine: 1\n",
      "goroutine: 0\n"
     ]
    }
   ],
   "source": [
    "import (\n",
    "    \"fmt\"\n",
    ")\n",
    "\n",
    "for i := 0; i < 3; i++ {\n",
    "    go func(id int) {\n",
    "        fmt.Println(\"goroutine:\", id)\n",
    "    }(i)\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go (lgo)",
   "language": "go",
   "name": "lgo"
  },
  "language_info": {
   "file_extension": "",
   "mimetype": "",
   "name": "go",
   "version": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
